<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:inv="http://www.quattroclix.com/invoice"
    exclude-result-prefixes="xs inv" version="1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.quattroclix.com/invoice invoice.xsd">
    
    <xsl:output indent="yes"/>
    
    <xsl:template match="/inv:invoice">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master margin-bottom="7mm" margin-left="19mm" margin-right="18mm"
                    margin-top="10mm" master-name="invoice-page-master">
                    <fo:region-body margin-bottom="20mm" margin-top="40mm"/>
                    <fo:region-before extent="30mm"/>
                    <fo:region-after extent="10mm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="invoice-page-master" id="main-sequence">
                
                <fo:static-content flow-name="xsl-region-before">
                    <fo:block-container absolute-position="absolute" left="100mm">
                        <fo:block>
                            <fo:external-graphic src="quattroclix.png"/>
                        </fo:block>
                    </fo:block-container>
                </fo:static-content>
                
                <fo:static-content flow-name="xsl-region-after">
                    <fo:block font-size="10pt" border-top="solid" border-top-color="black" padding-top="2mm">
                        <fo:inline>Quattroclix SPRL - Avenue des Villas 75 bte 16 1190 Bruxelles - TVA: BE
                            0894.266.853
                        </fo:inline>
                    </fo:block>
                    <fo:block font-size="10pt">
                        <fo:inline>IBAN: BE24 6511 4039 1338 - BIC: KEYTBEBB</fo:inline>
                    </fo:block>
                </fo:static-content>
                
                <fo:flow flow-name="xsl-region-body">
                    
                    <fo:block font-size="22pt" text-align="center" width="100%" space-before="1cm" space-after="1cm">
                        PREMIER RAPPEL
                    </fo:block>
                    
                    <fo:block font-size="14pt" space-before="1cm">
                        Après vérification de nos comptes au 04/10/2010, nous n’avons à ce jour constaté aucun paiement de votre part. 
                    </fo:block>
                    <fo:block font-size="14pt" space-before="1cm">
                        Nous vous prions de bien vouloir régler le montant de cette facture sur notre compte au plus vite et ainsi d’éviter une mise en demeure ou d’autres procédures juridiques.
                    </fo:block>
                    <fo:block font-size="14pt" space-before="1cm" space-after="1cm">
                        Si votre paiement a croisé ce rappel, veuillez considérer la présente comme nulle et non avenue.
                    </fo:block>
                    
                    
                    <fo:table width="100%" table-layout="fixed">
                        <fo:table-column column-width="50%"/>
                        <fo:table-column column-width="50%"/>
                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:table width="75%" font-size="16pt" table-layout="fixed">
                                        <fo:table-column column-width="40%"/>
                                        <fo:table-column column-width="60%"/>
                                        <fo:table-body>
                                            <fo:table-row>
                                                <fo:table-cell>
                                                    <fo:block>Facture</fo:block>
                                                </fo:table-cell>
                                                <fo:table-cell>
                                                    <fo:block>
                                                        <xsl:value-of select="/inv:invoice/@number"/>
                                                    </fo:block>
                                                </fo:table-cell>
                                            </fo:table-row>
                                            <fo:table-row>
                                                <fo:table-cell>
                                                    <fo:block>Date</fo:block>
                                                </fo:table-cell>
                                                <fo:table-cell>
                                                    <fo:block>
                                                        <xsl:call-template name="formatdate">
                                                            <xsl:with-param name="date"><xsl:value-of select="/inv:invoice/inv:issue-date/text()"/></xsl:with-param>
                                                        </xsl:call-template>
                                                    </fo:block>
                                                </fo:table-cell>
                                            </fo:table-row>
                                        </fo:table-body>
                                    </fo:table>
                                    
                                </fo:table-cell>
                                <fo:table-cell>
                                    <fo:block font-size="14pt">
                                        <fo:block>
                                            <xsl:value-of select="/inv:invoice/inv:client/inv:name/text()"/>
                                        </fo:block>
                                        <fo:block>
                                            <xsl:value-of select="/inv:invoice/inv:client/inv:street1/text()"/>
                                        </fo:block>
                                        <fo:block>
                                            <fo:inline>
                                                <xsl:value-of select="/inv:invoice/inv:client/inv:zip/text()"/>
                                            </fo:inline>
                                            <fo:inline padding-left="3mm">
                                                <xsl:value-of select="/inv:invoice/inv:client/inv:city/text()"/>
                                            </fo:inline>
                                        </fo:block>
                                        <xsl:if test="/inv:invoice/inv:client/inv:vat/text()">
                                            <fo:block>TVA:
                                                <xsl:value-of select="/inv:invoice/inv:client/inv:vat/text()"/>
                                            </fo:block>
                                        </xsl:if>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                    
                    <fo:block space-before="10mm">
                        <fo:block font-size="10pt" margin-top="10mm">
                            <fo:table border-style="solid" table-layout="fixed" width="100%">
                                <fo:table-column column-width="95mm"/>
                                <fo:table-column column-width="15mm"/>
                                <fo:table-column column-width="25mm"/>
                                <fo:table-column column-width="15mm"/>
                                <fo:table-column column-width="23mm"/>
                                <fo:table-header background-color="silver" text-align="center">
                                    <fo:table-row>
                                        <fo:table-cell border-style="solid" padding="1mm">
                                            <fo:block font-weight="bold">Product Description</fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border-style="solid" padding="1mm">
                                            <fo:block font-weight="bold">Quant.</fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border-style="solid" padding="1mm">
                                            <fo:block font-weight="bold">Unit Price (eur)</fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border-style="solid" padding="1mm">
                                            <fo:block font-weight="bold">VAT</fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border-style="solid" padding="1mm">
                                            <fo:block font-weight="bold">Price (eur)</fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                </fo:table-header>
                                <fo:table-body>
                                    <xsl:for-each select="//inv:line">
                                        <fo:table-row>
                                            <fo:table-cell padding="1mm">
                                                <fo:block>
                                                    <xsl:value-of select="inv:description/text()"/>
                                                </fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell padding="1mm">
                                                <fo:block text-align="center">
                                                    <xsl:value-of select="inv:quantity/text()"/>
                                                </fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell padding="1mm">
                                                <fo:block text-align="right" padding-right="3mm">
                                                    <xsl:value-of select="inv:unit-price/text()"/>
                                                </fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell padding="1mm">
                                                <fo:block text-align="right" padding-right="3mm">
                                                    <xsl:value-of select="100*number(inv:vat/text())"/>&#37;
                                                </fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell padding="1mm">
                                                <fo:block text-align="right">
                                                    <xsl:value-of
                                                        select="format-number(inv:quantity * inv:unit-price + inv:quantity * inv:unit-price * inv:vat, '.00')"/>
                                                </fo:block>
                                            </fo:table-cell>
                                        </fo:table-row>
                                    </xsl:for-each>
                                    
                                    <fo:table-row>
                                        <fo:table-cell number-columns-spanned="5" padding="1mm" padding-top="2mm">
                                            <fo:block font-style="backslant" text-align="right" font-weight="bold">
                                                <fo:inline padding-right="7mm">Total HTVA</fo:inline>
                                                <fo:inline>
                                                    <xsl:call-template name="total">
                                                        <xsl:with-param name="total">0</xsl:with-param>
                                                        <xsl:with-param name="count">
                                                            <xsl:value-of select="count(/inv:invoice/inv:line)"/>
                                                        </xsl:with-param>
                                                        <xsl:with-param name="vat">0</xsl:with-param>
                                                    </xsl:call-template>
                                                </fo:inline>
                                                <fo:inline>eur</fo:inline>
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row>
                                        <fo:table-cell number-columns-spanned="5" padding="1mm" padding-top="2mm">
                                            <fo:block font-style="backslant" text-align="right" font-weight="bold">
                                                <fo:inline padding-right="7mm">Total TVAC</fo:inline>
                                                <fo:inline>
                                                    <xsl:call-template name="total">
                                                        <xsl:with-param name="total">0</xsl:with-param>
                                                        <xsl:with-param name="count">
                                                            <xsl:value-of select="count(/inv:invoice/inv:line)"/>
                                                        </xsl:with-param>
                                                        <xsl:with-param name="vat">1</xsl:with-param>
                                                    </xsl:call-template>
                                                </fo:inline>
                                                <fo:inline>eur</fo:inline>
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                </fo:table-body>
                            </fo:table>
                        </fo:block>
                    </fo:block>
                    
                    <fo:block space-before="1cm">
                        Avec nos meilleures salutations.
                    </fo:block>
                    <fo:block space-before="1cm">
                        Pierre Moermans
                    </fo:block>
                </fo:flow>
                
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
    
    <xsl:template name="total">
        <xsl:param name="total"/>
        <xsl:param name="count"/>
        <xsl:param name="vat"/>
        
        <xsl:variable name="quantity" select="number(/inv:invoice/inv:line[number($count)]/inv:quantity)"/>
        <xsl:variable name="unit-price" select="number(/inv:invoice/inv:line[number($count)]/inv:unit-price)"/>
        
        <xsl:variable name="vat_amount" select="/inv:invoice/inv:line[number($count)]/inv:vat"/>
        <!--<xsl:variable name="vat_amount" select="number(concat('0.',substring(/inv:invoice/inv:line[number($count)]/inv:vat,0,3)))"/>-->
        
        <xsl:variable name="prod" select="number($quantity * $unit-price * (1 + $vat_amount * $vat))"/>
        <xsl:variable name="newTotal" select="number($total + $prod)"/>
        
        <xsl:choose>
            <xsl:when test="number($count - 1) > 0 ">
                <xsl:call-template name="total">
                    <xsl:with-param name="total">
                        <xsl:value-of select="$newTotal"/>
                    </xsl:with-param>
                    <xsl:with-param name="count">
                        <xsl:value-of select="number($count - 1)"/>
                    </xsl:with-param>
                    <xsl:with-param name="vat">
                        <xsl:value-of select="$vat"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="toWrite">
                    <xsl:value-of select="format-number($newTotal,'.00')"/>
                </xsl:variable>
                <xsl:if test="string-length($toWrite) &lt; 8">
                    <xsl:value-of select="concat(' ', format-number($newTotal,'.00'))"/>
                </xsl:if>
                <xsl:if test="string-length($toWrite) &gt; 7">
                    <xsl:value-of select="format-number($newTotal,'.00')"/>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="formatdate">
        <xsl:param name="date"/>
        <xsl:variable name="year"><xsl:value-of select="substring($date,0,5)"/></xsl:variable>
        <xsl:variable name="month"><xsl:value-of select="substring($date,6,2)"/></xsl:variable>
        <xsl:variable name="day"><xsl:value-of select="substring($date,9,2)"/></xsl:variable>
        <xsl:value-of select="concat($day,'/',$month,'/',$year)"/>
    </xsl:template>
</xsl:stylesheet>
